package com.zuxia.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zuxia.dto.commission.CommissionDto;
import com.zuxia.entity.Balance;
import com.zuxia.entity.Commission;
import com.zuxia.exception.ZuxiaBusinessException;
import com.zuxia.mapper.BalanceMapper;
import com.zuxia.mapper.CommissionMapper;
import com.zuxia.param.BalanceParam;
import com.zuxia.service.BalanceService;
import com.zuxia.service.CommissionService;
import com.zuxia.utils.ResultCodeEnum;
import com.zuxia.utils.TimeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoli
 * @since 2024-11-19
 */
@Service
public class CommissionServiceImp extends ServiceImpl<CommissionMapper, Commission> implements CommissionService {
    @Resource
    private CommissionMapper commissionMapper;
    @Resource
    private BalanceMapper balanceMapper;

    @Override
    public CommissionDto getNotEntry() {
        List<Commission> commissions = commissionMapper.selectList(null);
        //一个对象装待入账的钱
        BigDecimal entry=BigDecimal.ZERO;
        CommissionDto result = new CommissionDto();

        for (Commission commission : commissions) {
            //查询已完成的订单，到手的收益(0-订单未完成收益路上，1-订单完成到手的收益，2-订单售后退货收益失效)
            if(commission.getCommissonStatus()==1){
                //查询待入账(默认为待入账-0，添加到入账-1，未入账-2)
                if(commission.getCommissionAddStatus()==0){
                    BigDecimal commissionMoney=commission.getCommissionMoney();
                    entry=entry.add(commissionMoney);
                }else{
                    entry=entry.add(null);
                }
//                result.setNotEntry(entry);
            }
            result.setNotEntry(entry);
        }
        //没有入账就为待入账的钱
        return result;
    }

    @Override
    public boolean Entry(String commissionId) {
        Commission commission = commissionMapper.selectById(commissionId);
        Balance balance = new Balance();
        //判断是否存在
        if(commission.getCommissionId()==null){
            throw new ZuxiaBusinessException(ResultCodeEnum.COMMISSION_IS_NULL);
        }
        //判断是否入账(默认为待入账-0，添加到入账-1，未入账-2)
        if(commission.getCommissionAddStatus()!=0){
            throw new ZuxiaBusinessException(ResultCodeEnum.COMMISSION_IS_NOT_ENTRY);
        }else{
            commission.setCommissionAddStatus(1);
            //状态为已入账
            commissionMapper.updateById(commission);
            //添加收支表记录，收入记录

            balance.setBalanceId(String.valueOf(IdUtil.getSnowflakeNextId()));
            balance.setUserId(commission.getUserId());
            balance.setBalanceMoney(commission.getCommissionMoney());
            balance.setBalanceStatus(0);//入账
            Long time=System.currentTimeMillis();
            String yMDHMS = TimeUtils.getY_M_D_H_M_S(time);
            balance.setBalanceTime(yMDHMS);
            balanceMapper.insert(balance);
            return true;
        }
    }
}
